Skip to content

perf(csr): index a hub's list for closing edges with a hub at both ends - #233

Open
prrao87 wants to merge 1 commit into
mainfrom
perf/closing-edge-hub-index
Open

prrao87 wants to merge 1 commit into
mainfrom
perf/closing-edge-hub-index

Conversation

@prrao87

@prrao87 prrao87 commented Nov 28, 2026

Copy link
Copy Markdown
Member

Part of #223.

Why

#228 made a closing edge (the last hop of a cycle, whose target is already bound) read the shorter of its two ends' lists. That fixed a hub at one end, but not a hub at both: if the source has 100,000 outgoing edges and the target 100,000 incoming ones, the shorter list is still 100,000 long, and every row scans it. In the shape suite, a triangle through a hub at both ends got 24 times slower in memory mode (16 times in lazy mode) as the hubs' degree rose from 10 to 100,000.

What changed

  • A long list that comes up again gets an index. When the list a closing edge would read is longer than 256 entries, the first row in a batch that reads it scans it, as before. The second builds an index of that node's neighbours to the edge rows that reach them, and every later row in the batch answers with one lookup. A list that comes up only once costs no more than it did.
  • Rows, edge rows and filters are unchanged. The index keeps every edge row, including parallel edges, and leaves out deleted ones, as a scan does. A row picks scan or index where it starts, so a row that pauses partway through its output resumes the same way, and the index path keeps the scan's work budget.
  • Nothing else compiles differently. Only the closing-edge copy of the expand loop changes; every other hop compiles as before.
  • Both thresholds in this code (32 and 256) now explain their values in their doc comments, and the operator docs describe the index.

The shape suite's both-ends headline drops from 24× to 4.1× (memory) and from 16× to 3.2× (lazy), but not to 1. Each row now costs the same at any degree; what remains is a one-time cost per batch, one scan of the hub's list and one index build. Removing that would mean precomputing hub indexes when the CSR is built, which is a bigger change, so I've left the #223 item "A closing edge costs the same whatever the degree of its endpoints" unchecked.

Validation

  • cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings, make test (1,822 passed), the strict docs build and typos pass locally. A new unit test runs five closing-edge rows between two hubs (with a parallel edge, another target and a missing one) and checks that the index gives exactly the scan's rows and edge rows, including with one row per batch so a row's indexed edges resume after a pause. CI has not run yet.
  • All three suites, per CONTRIBUTING.md: release wheels from main (a480fd5) and this branch (2501264), alternating main and branch twice each, memory and lazy. Every answer check and every shape-suite phrasing and plan check passed.

Shape suite headlines (small graph):

Group Headline Ideal memory: main memory: branch lazy: main lazy: branch
phrasing slowest / fastest of 8 phrasings 1 2.81x 2.84x 7.03x 7.02x
selectivity two filters: slowest / fastest end 1 14.54x 14.49x 11.57x 11.69x
selectivity hop order: slowest / fastest 1 1.52x 1.56x 1.41x 1.42x
count_rows slowest count / fastest rows <= 1 7.11x 7.17x 6.45x 6.55x
hubs cycle: degree 100000 / degree 10 1 1.01x 1.01x 1.00x 1.00x
hubs cycle, hub at both ends: degree 100000 / 10 1 23.67x 4.14x 16.26x 3.18x
hubs pattern predicate: degree 100000 / 10 1 1.01x 1.01x 1.00x 1.01x
hubs cycle at degree 100000: CSR / join <= 1 0.35x 0.35x 0.36x 0.36x
hubs predicate at degree 100000: CSR / join <= 1 0.05x 0.05x 0.10x 0.10x
optional filtered / unfiltered OPTIONAL MATCH <= 1 1.03x 1.03x 1.58x 1.56x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 0.93x 0.93x 0.98x 0.98x
wide_columns cross_filter: notes / ids about 1 4.10x 4.06x 16.90x 16.40x
wide_columns top_k: notes / ids about 1 2.92x 2.90x 13.90x 13.56x
wide_columns far_filter: notes / ids about 1 1.37x 1.36x 6.38x 6.51x
wide_columns all: notes / ids > 1 4.37x 4.33x 17.12x 16.88x
early_exit LIMIT 10 / all rows, ids << 1 0.36x 0.36x 0.41x 0.41x
early_exit LIMIT 10 / all rows, notes << 1 0.62x 0.64x 0.90x 0.86x

Only the both-ends queries moved by 15% or more: degree 10,000 by −61% (memory) and −52% (lazy), and degree 100,000 by −82% and −80%. At degree 100,000 the index adds about 3 MiB of peak memory in memory mode and 6 MiB in lazy mode.

SF1 suites:

Suite Mode Geomean branch / main main run 2 / run 1
graph-benchmark memory 1.001 0.992
graph-benchmark-ldbc memory 1.000 1.004
graph-benchmark-ldbc lazy 0.991 0.981
graph-benchmark-ldbc-complex memory 1.003 1.021
graph-benchmark-ldbc-complex lazy 1.000 0.993

No SF1 query moved by 10% or more.

Machine: Apple M5 MacBook Pro, 24 GB, macOS 26.6.2. Load average 1.7 to 8.8, with the high end right after the release builds; another terminal app was also running.

Every SF1 query

graph-benchmark, memory

Query main (ms) branch (ms) Change
query1 7.34 7.38 +1%
query2 11.89 12.14 +2%
query3 2.73 2.68 -2%
query4 1.61 1.62 +0%
query5 1.63 1.62 -1%
query6 1.90 1.87 -1%
query7 3.67 3.70 +1%
query8 12.74 12.85 +1%
query9 12.12 12.15 +0%

Geomean branch/main: 1.001; main run 2 / run 1: 0.992 (largest single-query main gap 5%)

graph-benchmark-ldbc, memory

Query main (ms) branch (ms) Change
query1 0.67 0.65 -3%
query2 0.58 0.56 -3%
query3 0.71 0.69 -3%
query4 0.55 0.54 -1%
query5 0.83 0.83 +1%
query6 0.46 0.46 +1%
query7 1.09 1.09 +0%
query8 0.67 0.67 -0%
query9 0.89 0.88 -1%
query10 0.78 0.77 -1%
query11 1.60 1.59 -1%
query12 0.79 0.79 -1%
query13 0.93 0.86 -7%
query14 0.56 0.56 +0%
query15 0.59 0.59 +0%
query16 0.71 0.72 +2%
query17 0.95 0.95 -0%
query18 0.73 0.73 +1%
query19 0.97 0.98 +1%
query20 2.09 2.13 +2%
query21 0.81 0.81 -0%
query22 0.75 0.75 +0%
query23 0.78 0.79 +2%
query24 0.73 0.74 +1%
query25 0.84 0.84 +1%
query26 0.79 0.81 +2%
query27 0.89 0.92 +3%
query28 1.42 1.44 +2%
query29 0.72 0.74 +2%
query30 19.35 19.83 +2%

Geomean branch/main: 1.000; main run 2 / run 1: 1.004 (largest single-query main gap 22%)

graph-benchmark-ldbc, lazy

Query main (ms) branch (ms) Change
query1 1.16 1.15 -1%
query2 1.47 1.41 -4%
query3 0.84 0.81 -3%
query4 0.81 0.80 -1%
query5 2.22 2.17 -2%
query6 0.77 0.75 -3%
query7 1.14 1.13 -1%
query8 1.76 1.74 -1%
query9 1.38 1.36 -2%
query10 0.97 0.96 -1%
query11 2.03 1.98 -3%
query12 6.67 6.61 -1%
query13 1.09 1.08 -1%
query14 0.72 0.71 -1%
query15 0.81 0.78 -3%
query16 1.15 1.12 -3%
query17 1.43 1.41 -1%
query18 1.17 1.16 -1%
query19 1.34 1.36 +2%
query20 16.55 16.67 +1%
query21 0.85 0.85 -0%
query22 0.95 0.96 +1%
query23 1.26 1.25 -1%
query24 1.08 1.08 +0%
query25 1.22 1.21 -0%
query26 1.08 1.08 -0%
query27 1.32 1.33 +1%
query28 2.04 2.04 -0%
query29 1.05 1.05 +0%
query30 19.42 19.92 +3%

Geomean branch/main: 0.991; main run 2 / run 1: 0.981 (largest single-query main gap 9%)

graph-benchmark-ldbc-complex, memory

Query main (ms) branch (ms) Change
query1 24.42 24.49 +0%
query2 2.49 2.49 -0%
query3 25.63 25.16 -2%
query4 5.81 5.82 +0%
query5 72.55 72.00 -1%
query6 20.46 20.74 +1%
query7 9.85 9.86 +0%
query8 2.37 2.38 +0%
query9 35.67 35.68 +0%
query10 18.14 18.30 +1%
query11 3.02 3.03 +0%
query12 15.94 16.44 +3%
query13 1.16 1.15 -0%
query14 245.37 246.58 +0%

Geomean branch/main: 1.003; main run 2 / run 1: 1.021 (largest single-query main gap 4%)

graph-benchmark-ldbc-complex, lazy

Query main (ms) branch (ms) Change
query1 34.80 34.89 +0%
query2 25.07 24.51 -2%
query3 169.09 165.27 -2%
query4 12.87 12.80 -0%
query5 94.81 94.20 -1%
query6 21.81 22.63 +4%
query7 13.58 13.70 +1%
query8 3.38 3.38 +0%
query9 230.94 225.33 -2%
query10 26.78 26.87 +0%
query11 5.37 5.37 +0%
query12 18.63 19.15 +3%
query13 1.42 1.42 -0%
query14 253.16 255.17 +1%

Geomean branch/main: 1.000; main run 2 / run 1: 0.993 (largest single-query main gap 3%)

Every shape-suite query

memory: small graph (20,000 accounts, 229,517 T edges, hub degrees [10, 1000, 10000, 100000])

Runs: main x2, branch x2.

Group Headline Ideal main branch
phrasing slowest / fastest of 8 phrasings 1 2.81x 2.84x
selectivity two filters: slowest / fastest end 1 14.54x 14.49x
selectivity hop order: slowest / fastest 1 1.52x 1.56x
count_rows slowest count / fastest rows <= 1 7.11x 7.17x
hubs cycle: degree 100000 / degree 10 1 1.01x 1.01x
hubs cycle, hub at both ends: degree 100000 / 10 1 23.67x 4.14x
hubs pattern predicate: degree 100000 / 10 1 1.01x 1.01x
hubs cycle at degree 100000: CSR / join <= 1 0.35x 0.35x
hubs predicate at degree 100000: CSR / join <= 1 0.05x 0.05x
optional filtered / unfiltered OPTIONAL MATCH <= 1 1.03x 1.03x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 0.93x 0.93x
wide_columns cross_filter: notes / ids about 1 4.10x 4.06x
wide_columns top_k: notes / ids about 1 2.92x 2.90x
wide_columns far_filter: notes / ids about 1 1.37x 1.36x
wide_columns all: notes / ids > 1 4.37x 4.33x
early_exit LIMIT 10 / all rows, ids << 1 0.36x 0.36x
early_exit LIMIT 10 / all rows, notes << 1 0.62x 0.64x
Query main ms branch ms Change main peak MiB branch peak MiB
phrasing/unnamed_start_inline 0.51 0.50 -1% 1.7 1.8
phrasing/unnamed_start_where 0.51 0.52 +1% 1.6 1.7
phrasing/unnamed_end_inline 0.51 0.51 -0% 1.6 1.7
phrasing/unnamed_end_where 0.52 0.52 -1% 1.6 1.7
phrasing/named_start_inline 1.40 1.41 +1% 10.3 10.8
phrasing/named_start_where 1.43 1.43 -0% 10.7 10.5
phrasing/named_end_inline 1.40 1.41 +1% 10.3 10.5
phrasing/named_end_where 1.43 1.43 +0% 10.3 10.4
selectivity/rows_fwd 10.92 10.85 -1% 2.5 2.8
selectivity/rows_rev 0.75 0.75 -0% 2.1 2.1
selectivity/hop_cheap_first 0.97 0.97 +0% 2.1 2.4
selectivity/hop_hub_first 1.47 1.51 +3% 2.3 2.4
count_rows/count_fwd 5.34 5.37 +0% 3.3 3.4
count_rows/count_rev 5.30 5.32 +0% 3.1 3.7
hubs/cycle_10 1.10 1.11 +1% 2.2 2.2
hubs/cycle_join_10 1.48 1.48 +0% 2.5 2.5
hubs/both_hubs_10 1.06 1.07 +1% 2.0 2.1
hubs/predicate_10 0.58 0.59 +0% 1.7 1.8
hubs/predicate_join_10 2.86 2.88 +1% 4.1 4.2
hubs/cycle_1000 1.12 1.12 +0% 2.1 2.4
hubs/cycle_join_1000 1.49 1.50 +0% 2.5 2.5
hubs/both_hubs_1000 1.25 1.07 -14% 2.0 2.2
hubs/predicate_1000 0.58 0.59 +1% 1.7 1.8
hubs/predicate_join_1000 2.96 2.96 -0% 5.3 5.1
hubs/cycle_10000 1.11 1.11 -0% 2.2 2.4
hubs/cycle_join_10000 1.79 1.80 +1% 3.3 3.2
hubs/both_hubs_10000 3.53 1.36 -61% 2.0 2.5
hubs/predicate_10000 0.58 0.59 +1% 1.7 1.8
hubs/predicate_join_10000 3.96 3.97 +0% 8.9 8.8
hubs/cycle_100000 1.12 1.12 +0% 2.1 2.3
hubs/cycle_join_100000 3.18 3.18 -0% 11.8 11.2
hubs/both_hubs_100000 25.04 4.42 -82% 2.0 5.2
hubs/predicate_100000 0.59 0.59 +1% 1.6 1.8
hubs/predicate_join_100000 11.14 11.12 -0% 55.0 52.2
optional/match 0.61 0.59 -3% 2.4 2.7
optional/optional 0.65 0.66 +1% 2.5 2.5
optional/optional_where 0.67 0.68 +1% 2.8 2.9
optional/match_where 0.72 0.73 +1% 2.1 2.4
wide_columns/far_filter_ids 2.14 2.16 +1% 2.8 3.2
wide_columns/far_filter_notes 2.93 2.94 +0% 26.6 27.4
wide_columns/cross_filter_ids 1.15 1.16 +1% 3.3 3.6
wide_columns/cross_filter_notes 4.72 4.69 -1% 126.9 124.0
wide_columns/top_k_ids 1.34 1.34 +0% 3.0 3.2
wide_columns/top_k_notes 3.90 3.88 -0% 64.8 63.8
wide_columns/all_ids 1.12 1.12 +1% 4.0 3.4
wide_columns/all_notes 4.88 4.86 -0% 186.7 187.6
early_exit/hop_limit_ids 0.38 0.39 +1% 1.9 2.0
early_exit/hop_all_ids 1.06 1.08 +1% 3.3 4.0
early_exit/hop_limit_notes 0.55 0.56 +2% 3.2 3.4
early_exit/hop_all_notes 0.89 0.88 -1% 20.1 20.4
Index main MiB branch MiB main build ms branch build ms
account 18.7 17.2 19 20
ring 30.1 30.6 27 28

main run-to-run geomean: 1.000; largest single-query gap 9%.

lazy: small graph (20,000 accounts, 229,517 T edges, hub degrees [10, 1000, 10000, 100000])

Runs: main x2, branch x2.

Group Headline Ideal main branch
phrasing slowest / fastest of 8 phrasings 1 7.03x 7.02x
selectivity two filters: slowest / fastest end 1 11.57x 11.69x
selectivity hop order: slowest / fastest 1 1.41x 1.42x
count_rows slowest count / fastest rows <= 1 6.45x 6.55x
hubs cycle: degree 100000 / degree 10 1 1.00x 1.00x
hubs cycle, hub at both ends: degree 100000 / 10 1 16.26x 3.18x
hubs pattern predicate: degree 100000 / 10 1 1.00x 1.01x
hubs cycle at degree 100000: CSR / join <= 1 0.36x 0.36x
hubs predicate at degree 100000: CSR / join <= 1 0.10x 0.10x
optional filtered / unfiltered OPTIONAL MATCH <= 1 1.58x 1.56x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 0.98x 0.98x
wide_columns cross_filter: notes / ids about 1 16.90x 16.40x
wide_columns top_k: notes / ids about 1 13.90x 13.56x
wide_columns far_filter: notes / ids about 1 6.38x 6.51x
wide_columns all: notes / ids > 1 17.12x 16.88x
early_exit LIMIT 10 / all rows, ids << 1 0.41x 0.41x
early_exit LIMIT 10 / all rows, notes << 1 0.90x 0.86x
Query main ms branch ms Change main peak MiB branch peak MiB
phrasing/unnamed_start_inline 0.65 0.65 -0% 2.1 2.3
phrasing/unnamed_start_where 0.65 0.65 -0% 2.1 2.3
phrasing/unnamed_end_inline 0.65 0.65 +0% 2.1 2.2
phrasing/unnamed_end_where 0.66 0.65 -1% 2.1 2.2
phrasing/named_start_inline 4.49 4.50 +0% 92.6 91.9
phrasing/named_start_where 4.54 4.54 -0% 91.9 92.3
phrasing/named_end_inline 4.49 4.48 -0% 92.3 92.9
phrasing/named_end_where 4.55 4.54 -0% 92.5 92.7
selectivity/rows_fwd 12.82 12.88 +1% 5.8 5.9
selectivity/rows_rev 1.11 1.10 -1% 3.5 3.4
selectivity/hop_cheap_first 1.29 1.29 +0% 2.8 2.8
selectivity/hop_hub_first 1.81 1.83 +1% 2.8 3.2
count_rows/count_fwd 7.15 7.19 +1% 7.4 7.7
count_rows/count_rev 7.14 7.22 +1% 7.5 7.2
hubs/cycle_10 2.00 2.02 +1% 3.9 4.5
hubs/cycle_join_10 3.32 3.35 +1% 5.7 5.4
hubs/both_hubs_10 1.57 1.58 +0% 2.3 2.6
hubs/predicate_10 1.39 1.40 +1% 3.5 3.0
hubs/predicate_join_10 5.65 5.63 -0% 13.6 15.9
hubs/cycle_1000 2.01 2.02 +0% 3.9 3.7
hubs/cycle_join_1000 3.34 3.35 +0% 5.8 5.8
hubs/both_hubs_1000 1.81 1.62 -10% 2.4 2.6
hubs/predicate_1000 1.38 1.41 +2% 3.4 3.1
hubs/predicate_join_1000 5.72 5.73 +0% 13.8 16.1
hubs/cycle_10000 2.00 2.04 +2% 3.5 3.9
hubs/cycle_join_10000 3.72 3.69 -1% 7.9 7.0
hubs/both_hubs_10000 3.96 1.92 -52% 2.4 3.2
hubs/predicate_10000 1.38 1.41 +2% 3.1 3.3
hubs/predicate_join_10000 6.43 6.44 +0% 19.2 18.9
hubs/cycle_100000 2.01 2.03 +1% 3.5 3.9
hubs/cycle_join_100000 5.51 5.58 +1% 23.5 23.0
hubs/both_hubs_100000 25.59 5.02 -80% 2.4 8.0
hubs/predicate_100000 1.39 1.42 +2% 2.6 3.7
hubs/predicate_join_100000 14.27 14.25 -0% 59.5 58.3
optional/match 0.76 0.76 +1% 3.0 2.9
optional/optional 0.81 0.81 +1% 3.1 2.9
optional/optional_where 1.27 1.27 +0% 4.2 4.6
optional/match_where 1.30 1.30 -0% 3.9 3.9
wide_columns/far_filter_ids 3.05 3.06 +0% 5.2 5.3
wide_columns/far_filter_notes 19.45 19.92 +2% 107.8 108.2
wide_columns/cross_filter_ids 1.44 1.45 +1% 4.0 4.0
wide_columns/cross_filter_notes 24.35 23.75 -2% 255.8 246.8
wide_columns/top_k_ids 1.67 1.67 +0% 3.9 3.9
wide_columns/top_k_notes 23.20 22.69 -2% 222.4 225.5
wide_columns/all_ids 1.43 1.43 -0% 5.9 6.2
wide_columns/all_notes 24.42 24.07 -1% 288.7 290.8
early_exit/hop_limit_ids 0.49 0.49 -1% 2.5 2.2
early_exit/hop_all_ids 1.18 1.18 +0% 5.9 5.8
early_exit/hop_limit_notes 5.72 5.75 +0% 108.5 109.0
early_exit/hop_all_notes 6.34 6.65 +5% 117.5 119.5
Index main MiB branch MiB main build ms branch build ms
account 19.1 19.0 19 18
ring 29.8 29.9 25 26

main run-to-run geomean: 0.995; largest single-query gap 10%.

🤖 Generated with Claude Code

Reading a closing edge from its shorter end (#228) leaves one case: both
ends are hubs, so either list is long and every row still scans one.
The shape suite's triangle through a hub at both ends grew 23x (memory)
from degree 10 to 100,000.

When the list a closing edge would read is longer than 256 entries, the
second row in a batch that reads the same list builds an index of its
neighbours to their edge rows, and later rows answer with one lookup.
The first sighting still scans, so a list that comes up once costs no
more than before. A row decides at its start, so a paused row resumes
as it began, and the index path keeps the scan's work budget. Only the
closing-edge copy of the loop changes. Both thresholds explain their
values in their doc comments.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Deploying lance-csr with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2501264
Status: ✅  Deploy successful!
Preview URL: https://0ae1266a.lance-csr.pages.dev
Branch Preview URL: https://perf-closing-edge-hub-index.lance-csr.pages.dev

View logs

@prrao87
prrao87 marked this pull request as ready for review November 28, 2026 15:36

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant